/**
 * @file sle_client.c
 * @brief SLE Client implementation for device discovery and connection management.
 *
 * This file contains the implementation of the SLE client, which is responsible for
 * discovering devices, managing connections, and handling communication with remote devices.
 *
 * @author Maker Studio
 * @license Apache 2.0
 *
 * This code is open-sourced under the Apache 2.0 license. When using open-source libraries
 * or code, please ensure to acknowledge the original authors and sources.
 */

#include "iot_errno.h"
#include "../inc/iot_gpio_ex.h"
#if CHIP_WS63
#include "pinctrl.h"
#include "los_base.h"
#else
#include "hi_gpio.h"
#include "hi_io.h"
#include "hi_task.h"
#include "hi_types_base.h"
#endif

#if CHIP_WS63
/**
 * @brief Set the pull-up/pull-down resistor for a specific pin.
 * @param id The pin ID.
 * @param val The pull-up/pull-down value.
 * @return IOT_SUCCESS on success, IOT_FAILURE on failure.
 */
unsigned int IoSetPull(unsigned int id, IotIoPull val)
{
    if (id >= PIN_NONE) {
        return IOT_FAILURE;
    }
    return uapi_pin_set_pull((pin_t)id, (pin_pull_t)val);
}

/**
 * @brief Set the function mode for a specific pin.
 * @param id The pin ID.
 * @param val The function mode value.
 * @return IOT_SUCCESS on success, IOT_FAILURE on failure.
 */
unsigned int IoSetFunc(unsigned int id, unsigned char val)
{
    if (id >= PIN_NONE) {
        return IOT_FAILURE;
    }
    return uapi_pin_set_mode((pin_t)id, val);
}

/**
 * @brief Sleep for a specified number of milliseconds.
 * @param ms The number of milliseconds to sleep.
 * @return IOT_SUCCESS on success, IOT_FAILURE on failure.
 */
unsigned int TaskMsleep(unsigned int ms)
{
    if (ms <= 0) {
        return IOT_FAILURE;
    }
    
    LOS_Msleep((unsigned int)ms);

    return IOT_SUCCESS;
}
#else
/**
 * @brief Set the pull-up/pull-down resistor for a specific pin.
 * @param id The pin ID.
 * @param val The pull-up/pull-down value.
 * @return IOT_SUCCESS on success, IOT_FAILURE on failure.
 */
unsigned int IoSetPull(unsigned int id, IotIoPull val)
{
    if (id >= HI_GPIO_IDX_MAX) {
        return IOT_FAILURE;
    }
    return hi_io_set_pull((hi_io_name)id, (hi_io_pull)val);
}

/**
 * @brief Set the function mode for a specific pin.
 * @param id The pin ID.
 * @param val The function mode value.
 * @return IOT_SUCCESS on success, IOT_FAILURE on failure.
 */
unsigned int IoSetFunc(unsigned int id, unsigned char val)
{
    if (id >= HI_GPIO_IDX_MAX) {
        return IOT_FAILURE;
    }
    return hi_io_set_func((hi_io_name)id, val);
}

/**
 * @brief Sleep for a specified number of milliseconds.
 * @param ms The number of milliseconds to sleep.
 * @return IOT_SUCCESS on success, IOT_FAILURE on failure.
 */
unsigned int TaskMsleep(unsigned int ms)
{
    if (ms <= 0) {
        return IOT_FAILURE;
    }
    return hi_sleep((hi_u32)ms);
}
#endif